home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ASMVEG.ZIP / ASMVEG.TXT next >
Text File  |  1996-12-23  |  15KB  |  337 lines

  1.  
  2. Assembly Language for Veggies (And C programmers)      Part 1.
  3.  
  4. So you wanna be an Assembly Language programmer? OK, no problem! this DOC is
  5. designed to introduce you to the basics of ASM and the concepts behind same. I
  6. will be providing examples and some demo routines along the way, along with
  7. cross refences and examples from other languages to clarify certain points.
  8.  
  9. OK, so here goes...
  10.  
  11. When you program in assembly language, you have complete and utter control of
  12. the computer, and everything it does. YOU get to choose EXACTLY it's behavior
  13. under your program. you can directly access any hardware and do anything - the
  14. only limit is your skill.
  15.  
  16. WHAT YOU GET
  17.  
  18. Basicly, assembly programs talk directly to the 8088, 8086, 80188, 80186,
  19. 80286, 80386, or 80486 IC inside your Machine. This is a custom chip designed
  20. by Intel and is called the CPU (Central Processing Unit). We begin by looking
  21. into these chips. your machine, depending upon model, will use one of these
  22. chips. XT's have either 8088's, 8086's or their NEC clones, the V20's and
  23. V30's. (The NEC Chips are 100% compatible), whilst the AT's use the 8018x
  24. series (Ratrely, but they are used!) or 80286 chip. The newer fast machines
  25. use 80386 or 80486 chips and hense their name.
  26. All the chips are "Upward Compatible" - that means that anything the 8088
  27. could do, all the chips can do too, except faster. The 186 and 286 added more
  28. instructions - the 386 & 486 can do those as well..  so you see that the 486
  29. is king of the mountain, but will do the exact same job of an 8088 (only about
  30. 30 times faster!) if required.
  31.  
  32. Because of this Upward compatibility, you see that we can write a program that
  33. works on an 8088 and expect it to execute correctly on any IBM design,
  34. regardless of CPU UNLESS we use instructions specificly for one of the later
  35. chips (Which is nearly never).
  36.  
  37. So, to program these chips, one requires an understanding of them.... Here
  38. goes. The chip has the ability to execute machine code instructions. This is
  39. the most important job of the chip. It reads an INSTRUCTION from computer
  40. memory, figures out what the instruction means, and executes it, then gets the
  41. next instruction. That is ALL that a CPU is capable of doing!!!!  As long as a
  42. computer is operating, it is doing this...from the first second you switch it
  43. on, until you switch it off again....
  44.  
  45. Even when a machine has "Crashed" it can still be doing something - and
  46. usually is - but what it is doing is useless and won't allow the operator a
  47. chance to send it instructions to tell it to stop it's useless activity. The
  48. only way to stop a CPU from doing it's job is to HOLD the RESET button on the
  49. computer down, or to switch power off.
  50.  
  51. Thus you see, you must have a logical set of instructions with a correct start
  52. point, and a correct end point. The CPU keeps track of what it is doing with a
  53. set of REGISTERS. the registers are of utmost importance to the programmer,
  54. for without them he would be lost.
  55.  
  56. Here are the registers of the 8088 series (common to all models):
  57.  
  58. AX, BX, CX, DX      SP, BP, SI, DI   CS, DS, ES, SS    IP, F.
  59.  
  60. The letters are the standard referance as used by common agreement. all
  61. registers are 16 bits wide - that is they can hold a number from 0 to FFFF
  62. hex. They are grouped according to use :
  63.  
  64. IP - Instruction Pointer, is used internally by the CPU to keep track of what
  65. instruction it should execute NEXT....IE a marker of where in memory it is up
  66. to. 
  67.  
  68. F - Flags, also internal to the CPU, is a set of 1 bit markers that can be
  69. either 0 or 1 to indicate a certain CPU status. The Flags have a set of
  70. instructions designed to read individual status Bits built into the CPU.
  71.  
  72. CS - code segment, the memory segment of the executing program. (more on
  73. segments to come in a tic..) - this will be set upon startup of your program
  74. and is usually NEVER touched.
  75.  
  76. DS - data segment, the default segment for which to get data from - used by
  77. some instructions for transferring data about in memory.
  78.  
  79. ES - Same as DS, but toally user definable.
  80.  
  81. SS - Stack segment - Like DS, but only for stack operations. not normally
  82. touched by user.. see section on stack.
  83.  
  84. SP - Stack pointer - a bit akin to IP, but for stack operations.
  85.  
  86. BP - Base pointer - general 16 bit register for user useage.
  87.  
  88. SI - source index - used by some instructions for data transfer. for user
  89. useage. 
  90.  
  91. DI - Destination Index - same as SI.
  92.  
  93. AX - Accumulator. 16 bit general register for user useage. all math conducted
  94. inside this register.
  95.  
  96. BX - Base - general register for user useage - also used in some operations.
  97.  
  98. CX - count - general register for user useage - also used in some block
  99. movement operations as a loop counter.
  100.  
  101. DX - Data - general register for user useage - also used in memory referance
  102. and 32 bit math operations.
  103.  
  104. To keep things flexible, AX, BX, CX and DX can be divided into 2 8 bit
  105. registers... Note: These are not extra, separate registers, simply a way of
  106. accessing the same register 8 bits at a time!!  The 8 bit versions are called
  107. AH and AL , BH and BL etc... not too obviously, AH is the top 8 bits of AX,
  108. whilst AL is the bottom 8 bits...
  109.  
  110. Thus a program that stores 67ac into AX could just as easily store 67 intoAH
  111. and ac into AL - it would result in the same thing - AX would now equal 67ac.
  112.  
  113. One important concept to be grasped is that the registers are just like
  114. pidgeon holes.... they just hold a number. That number can be an address, the
  115. ASCII code for a letter, the result of a math instruction or whatever. The CPU
  116. only knows it's got a number... thus, there's no such thing as:
  117.  
  118. Var
  119.  
  120.   cx : word;
  121.   al : char;
  122.  
  123. or similar...  It overcomes a big hassle in many languages... in PASCAL one
  124. can't take a number variable and drop it into the middle of a string, one must
  125. use the STR( function...  not in ASM... one just  umm....  uses it! thus there
  126. are no "conversion" functions built in, or needed.... makes things a LOT
  127. simpler at times!
  128.  
  129. As you ave gathered, the 8088 series are 16 bit CPU's - called this because
  130. all the registers are 16 bit, and the data paths inside the cips are 16 bit
  131. also! (Funny 'bout that)...  BUT they were designed to use up to 1 MB of
  132. memory. (Take my word for it) .... The problem is that 1 Meg requires 20 bits
  133. to count up all the combinations...  how does one count to 20 bits with 16 bit
  134. registers?  Impossible!  -  YES!! .... so the designers thought that instead
  135. of inventing a 20 bit CPU they'd design SEGMENTATION. This is one thing new
  136. programmers come to hate! It's easy if you follow it carefully, but more often
  137. than not people stuff it up. This is where the segment registers come into
  138. play.
  139.  
  140. Memory is accessed using a combination of 2 16 bit registers... the segment
  141. and the offset... Valid combinations include :  CS:IP (for where to get the
  142. next instruction from) SS:SP (stack location) DS:SI, ES:DI and more... Note
  143. that a SEGEMENT register must come first (CS, DS, ES, SS) - you can't do AX:DI
  144. - it just isn't allowed. This is a hardware restriction, but in practice it's
  145. not a hassle.
  146.  
  147. Here's the math for working out which address you're at...
  148.  
  149. The segment registers point to the start of a 64k "Chunk" of RAM, whilst the
  150. offset points to the byte within that chunk. 
  151.  
  152. (All addresses in HEX notation)
  153.  
  154. you can have many combinations that relate to the same physical address...
  155. Thus: 0000:0401 is the same as 0040:0001,  f000:a000 is the same as fa00:0000
  156.  
  157. Addition is performed inside the CPU to work things out thus:
  158.  
  159.      Segment register:    0000          0040
  160.  plus offset register:     0401          0001
  161. -------------------------------------------------
  162.                equals:    00401         00401
  163. --------------------------------------------------
  164.  
  165. note how the result is 5 hex digits long - that's 20 bits in binary. The
  166. segment is moved one digit along as it's a 64k chunk it points to. (64k = 4
  167. bits = 1 hex digit)
  168.  
  169. By the way, get used to hex, it's the generic way of referring to register
  170. contents.. It's always a 4 DIGIT number for a 16 bit register, a 2 DIGIT
  171. numver for 8 bit, or a 5 digit number for 20 bit. the conversion is thus:
  172.  
  173.            |          |           |           |           |
  174. Binary:     1  0  1  0  0  0  0  1  1  1  0  0  1  0  0  1
  175.  
  176. Take the nuber in groups of 4 bits. A hex digit is base 16 - there are 16
  177. possibilities per digit (Decimal offers 10 [0-9]) hex has 0-9 and a-f [16
  178. varietites]
  179.  
  180. you get 16 combinations in 4 bits - from 0 0 0 0   to  1 1 1 1 [0-f]
  181.  
  182. so the number above is: a1c9
  183.  
  184. Remember that each bit has a "weight" thus:
  185.  
  186. 8 4 2 1   -  weight
  187.  
  188. 0 1 1 0   -  hex number
  189.  
  190. to convert quickly, take a group of 4 bits, mentally ad the weights of all "1"
  191. bits - in this example 4+2 and the result is 6. The hex for this binary is 6.
  192. note that in hex addition, 9+1=a, not 10!!!   SO:
  193.  
  194. 1 1 1 1    =  8+4 (c) + 2 (e) +1 (f)  =  F hex.
  195.  
  196.  
  197. That is all the CPU provides for you to use!!!  (And all you need)... Here's
  198. how... 
  199.  
  200.  
  201. THE BASIC IBM PC
  202.  
  203. We begin out examples by looking at a basic IBM PC equiped with <say> a
  204. floppy, a hard drive, some RAM and a video card, running MS-DOS.
  205.  
  206. OK, when your program is started, it is given access to all available memory
  207. from wherever dos has currently used up to the end of physical memory. This
  208. could be as much as 600k or maybe even more under DOS 5.0, or as little as 30
  209. or 60k in a very small multitasking window. Your program has permission to do
  210. anything to this block of memory, and it's contents at load time are garbage.
  211.  
  212. Program begins execution at the first instruction in your program (CS:IP will
  213. initially point here ) and wanders through, following the program to the end.
  214.  
  215. In the IBM PC the CS, IP, DS, ES and SS:SP are all preset for you to valid,
  216. correct settings when your program is loaded. further, CS, DS, ES (and usually
  217. SS) will all be equal.
  218.  
  219. Because of the 64k segmentation limitation, everyone seems to do things in 64k
  220. chunks, and DOS is no exception. your program always begins at CS:0100 (The
  221. first 256 hex bytes are filled with information to be used by the program if
  222. needed) and the SS:SP is usuallyplaced at the very end of the segment (ie
  223. SS=CS, IP=FFFE)
  224.  
  225. ABOUT THE STACK
  226.  
  227. The stack is vital to the operation of any program. It is for holding
  228. temporary addresses during program execution, and can be used by the user or
  229. the CPU at any time. Thus, a valid stack must always be maintained. Whenever
  230. an instruction executes the equivilant of a BASIC GOSUB, the address of where
  231. to go upon RETURN is saved on the stack. This must be a 16 bit digit (CS:IP)
  232. thus the stack starts at FFFE and not FFFF. after a storage, the SP is
  233. decreased by 2, so it then points to FFFC. don't ask why it grows downward, it
  234. just does.... the lower the SP, the bigger the stack. Again, when the RETURN
  235. is executed, the SP has 2 added to it, and again becomes FFFE. 
  236. more on the stack later.
  237.  
  238. now it's time to see what is available to our program when it's run.
  239.  
  240. IBM thought they'd give us a set of interface routines for using the hardware
  241. they'd built in. Nice of them that, saves us from directly manipulating the
  242. hardware which is usually a tricky and wierd task! These are called the BIOS
  243. routines and are built into a chip on the computer's hardware. They are
  244. responsible for starting the computer when powered u, and also loading the
  245. operating system MS-DOS.
  246.  
  247. DOS also supplies a set of routines for working with DOS - these are called the
  248. DOS routines (No shit!) and are available whenever DOS is in memory.
  249.  
  250. There's stuff like reading and writing to disk, screen, etc. getting emory
  251. sizes etc and all sorts. See a good ASM book for details - there's hundreds of
  252. them and take about 200 pages of text to fully cover - I'm not typing that lot
  253. out again!!!
  254.  
  255. In fact, most of your programs will simply be loading up and calling these
  256. routines... Here's a simple example (Type this into A86, it'll work !!)
  257.  
  258.  
  259. ; Demo program1
  260.  
  261. begin:         jmp start
  262.  
  263. string         db 'Hi there!!$'
  264.  
  265. start:         mov dx,offset string
  266.          mov ah,09
  267.          int 021
  268.          int 020
  269.  
  270.  
  271. now that will be very confusing to you, but it's a simple program in assembler
  272. (Can you guess what it does?) Let's look at it line by line.
  273.  
  274. ; Demo program1   ---  any text after a ; is ignored - you're actually telling
  275. the assembler here, not writing 8088 code. This could be left out without any
  276. problem. It does not effect the size of the final code.
  277.  
  278. begin: jmp start     Here's our first instruction. Begin and start are LABELS
  279. used by the assembler to refer to an address... note how there's no hardware
  280. addresses written in... I could have said simply JMP CS:010F but it's much
  281. easier to use a label. That way if I added more between Begin: and start: i
  282. would not have to recalculate the address. The assembler works out the address
  283. at assembly time and substitutes it instead.
  284.  
  285. string  db '.....$'     String is another label. db means define byte. this is
  286. how we reserve memory. everything between the quotes is stored into the
  287. program and appears in memory at load time referenced by the string label.
  288.  
  289. start: mov dx,offset string  this loads the DX register with the ADDRESS of the
  290. string label. Note the word offset. This means you want the address of the
  291. label, not what is at that address.
  292.  
  293. mov ah,09   - loads the AH register with 09 hex. This is needed by the next
  294. instruction. 
  295.  
  296. INT 021    -  Call MS-DOS's built in routines They see that AH=09, decide that
  297. you want a write string to screen routine and display the text starting at the
  298. location in DX (Well, really DS:DX, but as I said, DS was setup for us before
  299. the program began) until it sees a $ symbol. The routine is written to return
  300. to our program when the $ symbol is encountered. the $ is not written to
  301. the screen. 
  302.  
  303. INT 020   - call another DOS routine. This one returns control to the calling
  304. program (In most cases Command.com)
  305.  
  306.  
  307. To fully understand all about what the hell is going on with all these INT's,
  308. I strongly suggest you invest in one of these books:
  309.  
  310. The Peter Norton Programmer's Guide to the IBM PC - Peter Norton. Try to get
  311. edition #2 but if you can only get a first ed copy or one'ds going cheap grab
  312. it - they're pretty good (I still use an ed.1 copy!)
  313.  
  314. Advanced MS-DOS - Ray Duncan. Only buy 2nd ed. 1st ed. was fairly limited and
  315. not really worth the money - it lacks any coverage above dos 3.0....
  316.  
  317. Nothing else is worth your money. I'll make the occasinal page cross referance
  318. (esp. to the Norton book which I feel is the better of the two) from time to
  319. time..  
  320.  
  321. ALSO
  322.  
  323. Scab from your favourite leeching BBS a copy of A86 V3.21 or later, and D86 to
  324. go with it... this is the assembler I'll be using in the future... I'll
  325. consider demonstrating MASM if you really want me too, but I don't know a hell
  326. of a lot about it and don't really want to learn... I only know enuf to know
  327. what a basic program might need.
  328.  
  329. This brings lesson 1 pretty much to a close... get yourself one of these
  330. books, delve into it, get A86, type in the demo, absorb as much as you can
  331. then write me back with your questions and problems!
  332.  
  333. I'll be starting lesson 2 soon!....   Cya there.
  334.  
  335.                                                           .\\erlin
  336.  
  337.